package com.googlecode.mysqlcommenter;

import java.util.List;
import java.util.Map;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class Util {
   private Util(){}

   public static TableModel transformToTableModel(List<Map<String, String>> queryResults){
      if(queryResults.isEmpty()){
         return new DefaultTableModel(
               new String[][] { { " ", " " }, { " ", " " } },
               new String[] { " ", " " });
      }

      Map<String, String> first = queryResults.get(0);
      String[] headers = first.keySet().toArray(new String[first.size()]);
      Object[][] data = new Object[queryResults.size()][headers.length];
      for (int i = 0; i < data.length; i++) {
         Object[] row = queryResults.get(i).values().toArray();
         for (int j = 0; j < row.length; j++) {
            data[i][j]=row[j];
         }
      }
      return new DefaultTableModel(data,headers);
   }

}
